VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
Begin VB.Form frmMarcas 
   Caption         =   "ABM Marcas"
   ClientHeight    =   4380
   ClientLeft      =   165
   ClientTop       =   450
   ClientWidth     =   7185
   LinkTopic       =   "Form1"
   ScaleHeight     =   4380
   ScaleWidth      =   7185
   StartUpPosition =   2  'CenterScreen
   Begin VB.ListBox lstMarcas 
      Height          =   3570
      Left            =   120
      TabIndex        =   6
      Top             =   600
      Width           =   2415
   End
   Begin VB.Frame frameMarca 
      Caption         =   "Marcas"
      Height          =   1335
      Left            =   2640
      TabIndex        =   1
      Top             =   600
      Width           =   4455
      Begin VB.TextBox txtId 
         Enabled         =   0   'False
         Height          =   375
         Left            =   1560
         TabIndex        =   3
         Top             =   240
         Width           =   2415
      End
      Begin VB.TextBox txtNombre 
         Height          =   375
         Left            =   1560
         MaxLength       =   50
         TabIndex        =   2
         Top             =   720
         Width           =   2415
      End
      Begin VB.Label lblId 
         Caption         =   "Id:"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   255
         Left            =   240
         TabIndex        =   5
         Top             =   360
         Width           =   375
      End
      Begin VB.Label lblNombre 
         Caption         =   "Nombre:"
         BeginProperty Font 
            Name            =   "MS Sans Serif"
            Size            =   8.25
            Charset         =   0
            Weight          =   700
            Underline       =   0   'False
            Italic          =   0   'False
            Strikethrough   =   0   'False
         EndProperty
         Height          =   255
         Left            =   240
         TabIndex        =   4
         Top             =   840
         Width           =   855
      End
   End
   Begin MSComctlLib.Toolbar tbrPrincipal 
      Align           =   1  'Align Top
      Height          =   420
      Left            =   0
      TabIndex        =   0
      Top             =   0
      Width           =   7185
      _ExtentX        =   12674
      _ExtentY        =   741
      ButtonWidth     =   609
      ButtonHeight    =   582
      Appearance      =   1
      _Version        =   393216
      BeginProperty Buttons {66833FE8-8583-11D1-B16A-00C0F0283628} 
         NumButtons      =   7
         BeginProperty Button1 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Key             =   "sbtAgr"
            Object.ToolTipText     =   "Agregar"
            ImageIndex      =   1
         EndProperty
         BeginProperty Button2 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Key             =   "sbtMod"
            Object.ToolTipText     =   "Modificar"
            ImageIndex      =   2
         EndProperty
         BeginProperty Button3 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Key             =   "sbtBus"
            Object.ToolTipText     =   "Buscar"
            ImageIndex      =   3
         EndProperty
         BeginProperty Button4 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Key             =   "sbtGra"
            Object.ToolTipText     =   "Grabar"
            ImageIndex      =   4
         EndProperty
         BeginProperty Button5 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Key             =   "sbtCan"
            Object.ToolTipText     =   "Cancelar"
            ImageIndex      =   5
         EndProperty
         BeginProperty Button6 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Key             =   "sbtBor"
            Object.ToolTipText     =   "Borrar"
            ImageIndex      =   6
         EndProperty
         BeginProperty Button7 {66833FEA-8583-11D1-B16A-00C0F0283628} 
            Key             =   "sbtImp"
            Object.ToolTipText     =   "Imprimir"
            ImageIndex      =   7
         EndProperty
      EndProperty
   End
   Begin VB.Menu mnActividades 
      Caption         =   "Actividades"
      Begin VB.Menu mnAgregar 
         Caption         =   "Agregar"
         Shortcut        =   ^A
      End
      Begin VB.Menu mnModificar 
         Caption         =   "Modificar"
         Shortcut        =   ^M
      End
      Begin VB.Menu mnBuscar 
         Caption         =   "Buscar"
         Shortcut        =   ^B
      End
      Begin VB.Menu mnGrabar 
         Caption         =   "Grabar"
         Shortcut        =   ^G
      End
      Begin VB.Menu mnCancelar 
         Caption         =   "Cancelar"
         Shortcut        =   ^Z
      End
      Begin VB.Menu mnBorrar 
         Caption         =   "Borrar"
         Shortcut        =   ^D
      End
   End
End
Attribute VB_Name = "frmMarcas"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Estado As Integer

Private Enum Estados
        eEsperarOperacion = 0
        eCargar
        eModificar
        eConsultar ' no se usa
        eBorrar
End Enum

Private Enum TipoDatos
    eString
    eInteger
    eCurrency
    eBoolean
    EDate
End Enum

Private Sub Refrescar_Formulario(Estado As Integer)

    With tbrPrincipal
        .Buttons(3).Visible = False
        .Buttons(7).Visible = False
    End With
            
    Select Case Estado
        Case Estados.eEsperarOperacion
                    
            lstMarcas.Enabled = True
            
            With frameMarca
                .Caption = "Marcas"
                .Enabled = False
            End With
            
            If lstMarcas.ListCount = 0 Then
                mnModificar.Enabled = False
                tbrPrincipal.Buttons(2).Enabled = False
            Else
                mnModificar.Enabled = True
                tbrPrincipal.Buttons(2).Enabled = True
            End If
            
            mnAgregar.Enabled = True
            mnGrabar.Enabled = False
            mnCancelar.Enabled = False
            mnBorrar.Enabled = False
            mnBuscar.Enabled = False
                        
            With tbrPrincipal
                .Buttons(1).Enabled = True
                .Buttons(4).Enabled = False
                .Buttons(5).Enabled = False
                .Buttons(6).Enabled = False
                .Buttons(1).ToolTipText = "Nueva Marca"
            End With
            
        Case Estados.eModificar
            With frameMarca
                .Caption = " Modificando "
                .Enabled = True
            End With
            
            lstMarcas.Enabled = False
            
            mnAgregar.Enabled = False
            mnModificar.Enabled = False
            mnGrabar.Enabled = True
            mnCancelar.Enabled = True
            mnBorrar.Enabled = True
            
            With tbrPrincipal
                .Buttons(1).Enabled = False
                .Buttons(2).Enabled = False
                .Buttons(4).Enabled = True
                .Buttons(5).Enabled = True
                .Buttons(6).Enabled = True
            End With

        Case Estados.eCargar
            With frameMarca
                .Caption = " Agregando "
                .Enabled = True
            End With
            
            LimpiarTXT Me
             
            lstMarcas.Enabled = False
            mnAgregar.Enabled = False
            mnModificar.Enabled = False
            
            mnGrabar.Enabled = True
            mnCancelar.Enabled = True
            mnBorrar.Enabled = False
            
            
            With tbrPrincipal
                .Buttons(1).Enabled = False
                .Buttons(2).Enabled = False
                .Buttons(4).Enabled = True
                .Buttons(5).Enabled = True
                .Buttons(6).Enabled = False
            End With

        Case Estados.eBorrar
        
            With frameMarca
                .Caption = "Marcas"
                .Enabled = False
            End With

            mnAgregar.Enabled = False
            mnModificar.Enabled = False
            
            mnGrabar.Enabled = True
            mnCancelar.Enabled = True
            mnBorrar.Enabled = False
            
          
            With tbrPrincipal
                .Buttons(1).Enabled = False
                .Buttons(2).Enabled = False
                .Buttons(4).Enabled = True
                .Buttons(5).Enabled = True
                .Buttons(6).Enabled = False
            End With
            
    End Select
    
    Exit Sub
End Sub

Private Sub Form_Load()
Dim oMarca As New clsMarcas
    
    FormsReconfig.Load_Reconfig Me

    oMarca.sel_Marcas
    If (oMarca.pCanReg > 0) Then
        CargarLista lstMarcas, oMarca.pRS, 0, 1
    End If
           
    If Not lstMarcas.ListCount = 0 Then
        cargarMarca_by_ID lstMarcas.ItemData(lstMarcas.ListIndex)
    End If
    
    Estado = Estados.eEsperarOperacion
    Refrescar_Formulario Estado
        
    Exit Sub
   
End Sub

Private Sub cargarMarca_by_ID(ByVal id As Integer)
Dim oRs As New ADODB.Recordset
Dim strSQL As String
Dim i As Integer
Dim oMarca As New clsMarcas
   
    oMarca.cargarMarca_by_ID id
    
    Set oRs = oMarca.pRS
    
    frameMarca.Visible = False
   
    LimpiarTXT Me
    
    If Not oRs.EOF Then
        If Not IsNull(oRs("id_marca")) Then txtId.TEXT = oRs("id_marca")
        If Not IsNull(oRs("marca")) Then txtNombre.TEXT = oRs("marca")
    End If
    
    frameMarca.Visible = True
    
    Set oRs = Nothing
    
    Exit Sub

End Sub

Private Sub actualizarMarca()
Dim currentIndex As Integer
Dim oMarca As New clsMarcas
Dim strError As String
Dim exito As Boolean
    
        currentIndex = lstMarcas.ListIndex
            
            exito = validarDatos(strError)
            If exito Then
                If oMarca.ActualizarMarca_by_ID(lstMarcas.ItemData(lstMarcas.ListIndex), _
                                                txtNombre.TEXT) Then
            
                    oMarca.sel_Marcas
                    CargarLista lstMarcas, oMarca.pRS, 0, 1
                    lstMarcas.Selected(currentIndex) = True
                
                    Estado = Estados.eEsperarOperacion
                    Refrescar_Formulario Estado
                Else
                    MsgBox "Error al actualizar la informacion de la marca"
                End If
            Else
                MsgBox strError
            End If
End Sub

Private Sub BorrarMarca()
Dim oMarca As New clsMarcas
    
        If oMarca.BorrarMarca_by_ID(lstMarcas.ItemData(lstMarcas.ListIndex)) Then
    
            oMarca.sel_Marcas
            If (oMarca.pCanReg > 0) Then
                CargarLista lstMarcas, oMarca.pRS, 0, 1
            Else
                lstMarcas.Clear
                LimpiarTXT Me
            End If
            
            Estado = Estados.eEsperarOperacion
            Refrescar_Formulario Estado
        Else
            MsgBox "No se pudo borrar la marca " & lstMarcas.ItemData(lstMarcas.ListIndex)
        End If
End Sub

Private Sub grabarMarca()
Dim oMarca As New clsMarcas
Dim strError As String
Dim exito As Boolean

    exito = validarDatos(strError)
    If exito Then
       If oMarca.grabarMarca(txtNombre.TEXT) Then
            oMarca.sel_Marcas
            If (oMarca.pCanReg > 0) Then
                CargarLista lstMarcas, oMarca.pRS, 0, 1
            End If
                    
            Estado = Estados.eEsperarOperacion
            Refrescar_Formulario Estado
         Else
            MsgBox "No se pudo crear la nueva marca"
        End If
     Else
        MsgBox strError
     End If
End Sub

Private Sub lstMarcas_Click()
    If lstMarcas.ListCount > 0 Then
        cargarMarca_by_ID lstMarcas.ItemData(lstMarcas.ListIndex)
    End If
End Sub

Private Sub mnAgregar_Click()
     Estado = Estados.eCargar
     Refrescar_Formulario Estado
End Sub

Private Sub mnBorrar_Click()
   
   BorrarMarca
End Sub

Private Sub mnCancelar_Click()
    
    If Not lstMarcas.ListCount = 0 Then
        cargarMarca_by_ID lstMarcas.ItemData(lstMarcas.ListIndex)
    End If
    
    Estado = Estados.eEsperarOperacion
    Refrescar_Formulario Estado
End Sub

Private Sub mnGrabar_Click()

   Select Case Estado
    Case Estados.eModificar
        actualizarMarca
    Case Estados.eCargar
        grabarMarca
    End Select

End Sub

Private Sub mnModificar_Click()

    Estado = Estados.eModificar
    Refrescar_Formulario Estado
    
End Sub

Private Sub tbrPrincipal_ButtonClick(ByVal Button As MSComctlLib.Button)
    
    Select Case Button.Key
        Case "sbtAgr"
            mnAgregar_Click
        Case "sbtMod"
            mnModificar_Click
        Case "sbtGra"
            mnGrabar_Click
        Case "sbtCan"
            mnCancelar_Click
        Case "sbtBor"
            mnBorrar_Click
    End Select
    
    Exit Sub
End Sub

Private Function validarDatos(ByRef strError As String) As Boolean
Dim exito As Boolean

    exito = True
    If exito And Trim(Me.txtNombre = "") Then
        exito = False
        strError = "Debe ingresar un nombre de marca antes de intentar grabar."
        Me.txtNombre.SetFocus
    End If
    If exito And Len(Me.txtNombre) > 50 Then
        exito = False
        strError = "El campo nombre de marca no debe contener mas de 50 caracteres"
        Me.txtNombre.SetFocus
    End If
    
    validarDatos = exito
End Function



